package com.ruoyi.blog.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.blog.pojo.AuthUser;
import com.ruoyi.blog.service.ArticleCommentService;
import com.ruoyi.blog.service.ArticleService;
import com.ruoyi.blog.service.UserService;
import com.ruoyi.blog.util.IdWorker;
import com.ruoyi.blog.util.SaveUseInfo;
import com.ruoyi.blog.util.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 网站首页
 */
@Controller
@RequestMapping("page")
@Slf4j
public class IndexController {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleCommentService articleCommentService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/random")
    @ResponseBody
    public String random() {
        return RandomStringUtils.randomNumeric(6);
    }

    /**
     * 首页
     */
    @GetMapping({"/index", "/", "/index.html"})
    public String pageIndex(Map<String, Object> map, HttpServletRequest request,
                            Authentication authentication, HttpSession session) {
        String username = UserInfoUtil.getUsername(authentication);
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        AuthUser user = userService.getOne(queryWrapper);
        SaveUseInfo.saveUserInfo(map, request, authentication, session, user);
        return "index";
    }

    /**
     * 文章详情页
     * @return
     */
    @GetMapping({"/detail"})
    public String pageDetail() {

        return "jie/detail";
    }

}
